<?php $this->Html->addCrumb('Monthly Sales Report'); ?>
<?php
echo $this->Html->css('datepicker/stylesheets/gui');
echo $this->Html->css('datepicker/stylesheets/timeframe');
?>  
  <!--[if lt IE 8]>
    <style type="text/css" media="screen">
      div.timeframe_calendar li a, div.timeframe_calendar table {
        display: inline;
      }
    </style>
  <![endif]-->
  <!--[if lt IE 7]>
    <link rel="stylesheet" href="stylesheets/ie6.css" type="text/css" media="screen" title="ie6" charset="utf-8"/>
<?php
echo $this->Html->css('datepicker/stylesheets/ie6');
?>
<![endif]-->
  
  <script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('table.CityTable th').click(
		function() {
			jQuery(this).parents('table.CityTable').children('tbody').toggle();
		}
	)
	/*jQuery('table.StateTable tr.statetablerow th').click(
		function() {
			jQuery(this).parents('table.StateTable').children('tbody').toggle();
		}
	)*/
});
</script>
<style type="text/css">
	table.StateTable{ width: 96%;  text-align:center;}
        table.CityTable{ width: 100%; border:0px  text-align:center;}
	table.StateTable{margin:20px;  solid #1C79C6;}

	table td{padding:5px; border: 0 none;
    border-collapse: collapse;}
	table.StateTable thead th{background: #1C79C6; padding: 10px; cursor:pointer; color:white; border: 0 none;
    border-collapse: collapse;}
	table.CityTable thead th{padding: 10px; background: #C7DBF1;cursor:pointer; color:black; border: 0 none;
    border-collapse: collapse;}
        table.StateTable td.nopad{padding:0;}
</style>
<style type="text/css">
td p span{color:#00B050;}
td.vendor h2 {font-size:20px; color: #00B050;}
.alt{
   background-color: #E6EEEE;
}
</style>

<div class="global-title">Monthly Sales Report</div>
<button>Date Range</button>
     <div id="example" style="display: none;">
      <h4>Please select a date range below:</h4>
      <div id="calendars"></div>
      <div id="calendar_form">
        <div id="labels">
          <label for="start">Date Range</label><label for="end"></label> (<a href="#" onclick="return false;" id="reset">reset</a>):
        </div>
          <form accept-charset="utf-8" action="<?php echo Configure::read('base_url');?>products/monthly_sales_report" method="post" id="productmessage">
        <div id="fields">
          <span>
            <input type="text" name="start" class="required" value="" id="start"/>
            &ndash;
            <input class="required" type="text" name="end" value="" id="end"/>
          </span>
        </div>

          <div style="padding-top:5px;">
              <input type="submit" class="button" name="button" id="button" value="Submit" />
          </div>
              </form>
      </div>
    </div>
    <?php  //echo $html->script(array("jquery.min.js","prototype.js","timeframe.js")); ?>
    <script type="text/javascript" charset="utf-8">
    //<![CDATA[
      new Timeframe('calendars', {
          months:3,
        startField: 'start',
        endField: 'end',
        earliest: new Date("October 13, 1975"),
        resetButton: 'reset' });
    //]]>
  </script>
  <script>
    jQuery("button").click(function () {
      jQuery("#example").slideToggle("slow");
    });
</script>
  <script type="text/javascript">
 jQuery(document).ready(function(){


		var v = jQuery("#productmessage").validate({
                    rules: {
                    }

		});


                 });


 </script>
<?php
function week($year, $week)
{
$from = date("d/m/Y", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
$to = date("d/m/Y", strtotime("{$year}-W{$week}-7")); //Returns the date of sunday in week
return "{$from} - {$to}.";
}
if(!empty($totalproduct)):
?>
<table class="StateTable" rules="all" cellpadding="0" border="0" cellspacing="0">
	<thead>
		<tr class="statetablerow">
			<th style="width:16%;">Week</th>
			<th style="width:17%;">Total Products Sold</th>
			<th style="width:17%;">Total Buyers</th>
			<th style="width:17%;">Total Sale Amount</th>
			<th style="width:17%;">Authentic Avenue Fees</th>
			<th style="width:16%;">Week-to-Week Growth</th>
		</tr>
	</thead>
	<tbody>

	<?php
        $i=0;
        $j=1;
        foreach($totalproduct as $totalproduct_value):
            $class = null;
            if ($j++ % 2 == 0){
            $class = ' class="alt invoicehead"';
            }else{
                $class = ' class="invoicehead"';
            }
             ?>
	  <tr>
         <td class="nopad" colspan="6">
            <table class="CityTable" rules="all" cellpadding="0" cellspacing="0" border="0">
               <thead>
                  <tr <?php echo $class;?>>
                    <th style="width:16%;"><?php echo $totalproduct_value[0]['week'];?></th>
		<th style="width:17%;"><?php echo $totalproduct_value[0]['tquantity'];?></th>
		<th style="width:17%;"><?php echo $totalbuyers[$i][0]['totalbuyer'];?></th>
		<th style="width:17%;">$<?php echo $totalsales[$i][0]['totalsale'];?></th>
		<th style="width:17%;">$<?php echo ($totalsales[$i][0]['totalsale']*0.1);?></th>
		<th style="width:16%;">
                    <?php
                    if(!empty($previousweek) AND $previousweek!='0.00'){
                        
                       $nettotal=((($totalsales[$i][0]['totalsale']-$previousweek)*100)/$previousweek);
                    }else{
                        $nettotal=100;
                    }
                       echo $nettotal.'%';
                       $previousweek=$totalsales[$i][0]['totalsale'];
                                       
                    ?>
		</th>
                  </tr>
               </thead>
               <tbody style="display:none;">
                   <tr style="background-color:#aaa;text-align: left;">
                     <td colspan="6">Week <?php echo $totalproduct_value[0]['week'];?>: <?php echo week(date('Y',  strtotime($totalproduct_value['Order']['add_date'])),$totalproduct_value[0]['week']);?></td>
                 </tr>
                  <tr style="background-color:#aaa;">
                     <td colspan="6">
                    <?php
                  $orderdetails = $this->requestAction('/products/orderdetails/'.$startdate.'/'.$enddate.'/'.$totalproduct_value[0]['week']);
                  //pr($orderdetails);
                  foreach($orderdetails as $orderdetail) {
                  //echo $comment['Comment']['title'];
                  //}*/
                  ?>
                <table  cellpadding="0" cellspacing="0" border="0" width="100%">
               <thead>
                   <tr>
                <td style="vertical-align:middle;" colspan="2">Order Ref: <?php echo $orderdetail['Order']['id'];?></td>
                <td style="vertical-align:middle;" colspan="2">Date: <?php echo date('Y-m-d',  strtotime($orderdetail['Order']['add_date']));?></td>
               <td style="vertical-align:middle;" colspan="2">Buyer : <?php echo $orderdetail['User']['username'];?></td>
                <td style="vertical-align:middle;" colspan="3">Total Order Amount: <?php echo ($orderdetail['Order']['product_cost']*$orderdetail['Order']['quantity']); ?></td>
                 </tr>
                 <tr style="background-color:#aaa;text-align: left;">
                     <td colspan="9">Order Details:</td>
                 </tr>
               <tr style="background:gray;">
               <td>&nbsp;</td>
                <td>(Item #) Titlle</td>
                <td>Buyer ID</td>
                <td>Qty</td>
                <td>Price</td>
                <td>Total Price</td>
                <td>Sale Date</td>
                <td>Traking #</td>
                <td>Carrier</td>
                </tr>
                 </thead>
               <tbody>
                <tr>
               <td style="vertical-align:middle;"><?php echo'<img src="'.Configure::read('base_url').'img/uploads/'.$orderdetail['Product']['products_thumbnailImage'].'" alt="'.$orderdetail['Product']['products_title'].'" width="80" border="0" />';?></td>
                <td style="vertical-align:middle;">(<?php echo $orderdetail['Product']['id']; ?>) <?php echo $orderdetail['Product']['products_title']; ?></td>
                <td style="vertical-align:middle;"><?php echo $orderdetail['User']['username']; ?></td>
                <td style="vertical-align:middle;"><?php echo $orderdetail['Order']['quantity']; ?></td>
                <td style="vertical-align:middle;"><?php echo $orderdetail['Order']['product_cost']; ?></td>
                <td style="vertical-align:middle;"><?php echo ($orderdetail['Order']['product_cost']*$orderdetail['Order']['quantity']); ?></td>
                <td style="vertical-align:middle;"><?php echo date('Y-m-d',  strtotime($orderdetail['Order']['add_date'])); ?></td>
                <td style="vertical-align:middle;"><?php echo $orderdetail['Order']['transaction_id']; ?></td>
                <td style="vertical-align:middle;">UPS Ground</td>
                </tr>
                </tbody>
                </table>
                <?php
                }
                ?>
                     </td>
                  </tr>
                                  
              </tbody>
           </table>
             
        </td>
     </tr>
    <tr>
    <td colspan="6" style="height:2px;background-color: #eee;">
    </td>
    </tr>
	<?php
        $i++;
        endforeach;
        ?>

	</tbody>
</table>
<?php else:?>
<br /><br />
<?php if($chk!='authentic'){ ?>
<h2>No Data Found.</h2>
<?php
}
?>
<br /><br /><br />
<?php endif;?>
<div class="paging">
    <?php
		//echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?> 
	<?php //echo $this->Paginator->numbers();?>
		
	<?php //echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
<?php echo $this->Html->css('table_css');?>